/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.version.undo.Change;

/**
 *
 * @author JKorreck
 */
public class MyItemPackages extends WebPage implements PropertyChangeListener {

//    ItemPackageForm itemPackageForm = new ItemPackageForm("itemPackageForm", false);
    String selectedID;

    public MyItemPackages() {

        MyItemPackageManager.getInstance().addPCL(this);

        add(new Label("selectedLabel", new PropertyModel(this, "selectedItemLabel")));
        add(new Label("title", "Packages"));
        add(new StyleSheetReference("stylesheet", MyItemPackages.class, "style.css"));

        this.setOutputMarkupId(true);

        this.addListView();

        List<IColumn> columns = new ArrayList<IColumn>();

        columns.add(new AbstractColumn(new Model("Actions")) {

            public void populateItem(Item cellItem, String componentId, IModel model) {
                cellItem.add(new ItemPackageActionPanel(componentId, model, MyItemPackages.this));
            }
        });
//
        columns.add(new PropertyColumn(new Model("ID"), "id", "id") {
//            @Override
//            public String getCssClass()
//            {
//                return "numeric";
//            }
        });
//
//        columns.add(new PropertyColumn(new Model("Category"), "category", "category") {
////            @Override
////            public String getCssClass()
////            {
////                return "numeric";
////            }
//        });
//
        columns.add(new PropertyColumn(new Model("Title"), "title", "title"));
        columns.add(new PropertyColumn(new Model("Price"), "packagePrice", "packagePrice"));
        columns.add(new PropertyColumn(new Model("Dicount"), "discount", "discount"));

         columns.add(new AbstractColumn(new Model("Content")) {

            public void populateItem(Item cellItem, String componentId, IModel model) {

                MyItemPackage p = (MyItemPackage) model.getObject();
                cellItem.add(new PackageContentPanel(componentId, model, p));
            }
        });
        add(new DefaultDataTable("table", columns, new SortableItemPackagesProvider(), 10));

        add(new Link("backLink") {

            @Override
            public void onClick() {
                setResponsePage(StartPage.class);
            }
        });
    }


    private void addListView() {
        List<MyItemPackage> allItems = MyItemPackageManager.getInstance().getAllItemPackages();
        add(new ListView("listview", allItems) {

            protected void populateItem(ListItem item) {
                MyItemPackage actItemPackage = (MyItemPackage) item.getModelObject();

                item.add(new Label("id", actItemPackage.getId()));
                item.add(new Label("title", actItemPackage.getTitle()));
                item.add(new Label("packageprice", "" + actItemPackage.getPackagePrice()));
                item.add(new Label("discount", "" + actItemPackage.getDiscount() + " %"));


                CheckBox box = new CheckBox("reserved", new PropertyModel<Boolean>(actItemPackage, "reserved"));
                box.setEnabled(false);

                item.add(box);
                item.add(MyItemPackages.this.addItemsListView(actItemPackage.getId()));
            }
        });
    }

    private ListView addItemsListView(String id) {
        ListView view;
        System.out.println("_________ID: " + id);
        view = new ListView("listview2", MyItemPackageManager.getInstance().getItemPackage(id).getItemList()) {

            protected void populateItem(ListItem item) {
                MyItem actItem = (MyItem) item.getModelObject();

                item.add(new Label("name", actItem.getName()));
                item.add(new  Label("weight", "" + actItem.getWeight()));
                item.add(new Label("unit", "" + actItem.getUnit()));
                item.add(new Label("price", "" + actItem.getPrice()));
            }

        };

        return view;
    }

    public String getSelected() {
        return selectedID;
    }

    public void setSelected(MyItemPackage selected) {
        addStateChange(new Change() {

            private final String old = MyItemPackages.this.selectedID;

            public void undo() {
                MyItemPackages.this.selectedID = old;

            }
        });
        this.selectedID = selected.getId();
    }

    public String getSelectedItemLabel() {
        if (selectedID == null) {
            return "No Item Selected";
        } else {
            return selectedID.toString();
        }
    }

    public void editSelectedItem(MyItemPackage editItem) {
//        this.itemPackageForm.setModelObject(editItem);
//        ((TextField) this.itemPackageForm.get("id")).setEnabled(false);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        this.setResponsePage(MyItemPackages.class);
    }
}
